package hongcaosp.app.android.contact.friends;

import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.target.ImageViewTarget;

import hongcaosp.app.android.R;
import hongcaosp.app.android.common.BaseHolder;
import hongcaosp.app.android.common.PresenterListener;
import hongcaosp.app.android.modle.bean.IUser;
import hongcaosp.app.android.modle.bean.IUserDetail;
import hongcaosp.app.android.video.player.VideoPresenter;
import xlj.lib.android.base.toastcompat.ToastManager;
import xlj.lib.android.base.utils.GlideUtils;
import xlj.lib.android.base.view.CircleImageView;

public class FriendHolder extends BaseHolder<IUserDetail> {
    private Context context;
    private ImageView userlogoIV;
    private TextView usernameTV,descripTV,productFansTV;
    private TextView followBTN;
    private VideoPresenter videoPresenter;
    private boolean showFans = true;

    public void setShowFans(boolean showFans) {
        this.showFans = showFans;
    }

    public FriendHolder(View itemView, Context context) {
        super(itemView);
        videoPresenter = new VideoPresenter();
        this.context = context;
        userlogoIV = itemView.findViewById(R.id.user_logo);
        usernameTV = itemView.findViewById(R.id.user_name);
        descripTV= itemView.findViewById(R.id.descrip);
        productFansTV = itemView.findViewById(R.id.product_fans);
        followBTN = itemView.findViewById(R.id.follow_btn);

        followBTN.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (getData() != null) {
                    if (getData().meFollowTa()) {
                        videoPresenter.cancelFollowUser(getData(), new PresenterListener() {
                            @Override
                            public void onSuccess() {
                                freshFollow();
                            }

                            @Override
                            public void onFail(String message) {
                                ToastManager.getInstance().showToast(message);
                            }
                        });
                    } else {
                        videoPresenter.followUser(getData(), new PresenterListener() {
                            @Override
                            public void onSuccess() {
                                freshFollow();
                            }

                            @Override
                            public void onFail(String message) {
                                ToastManager.getInstance().showToast(message);
                            }
                        });
                    }
                }
            }
        });
    }

    @Override
    public void setData(final IUserDetail iFriend) {
        super.setData(iFriend);

        GlideUtils.loadCircleImage(context,iFriend.getUserLogo(),userlogoIV,R.drawable.icon_head);
        usernameTV.setText(iFriend.getUserName());
        descripTV.setText(iFriend.getUserComment());
        if (showFans) {
            productFansTV.setVisibility(View.VISIBLE);
            productFansTV.setText(iFriend.worksCount() + " 作品   " + iFriend.getFansCount() + "粉丝");
        } else {
            productFansTV.setVisibility(View.GONE);
        }

        freshFollow();
    }

    private void freshFollow() {
        if(getData().meFollowTa()) {
            followBTN.setSelected(true);
            followBTN.setText("已关注");
        } else {
            followBTN.setSelected(false);
            followBTN.setText("关注");
        }
    }
}
